package com.noble.glamour.module.local.chat.msgtype.bean;

import android.os.Bundle;
import android.text.TextUtils;

import com.noble.glamour.bean.db.msg.FLetter;
import com.noble.glamour.bean.db.msg.FMessage;
import com.noble.mail.MessageConstant;
import com.noble.mail.utils.MsgIDUtils;
import com.noble.glamour.module.local.chat.utils.MsgUtils;
import com.noble.glamour.module.local.msgview.chatview.base.ChatPanelType;
import com.noble.glamour.module.logic.application.App;
import com.noble.glamour.module.logic.application.ModuleMgr;
import com.noble.glamour.module.util.TimeUtil;
import com.noble.glamour.ui.mail.item.MailItemType;
import com.noble.library.log.PLogger;
import com.noble.library.utils.TypeConvertUtil;

import org.json.JSONObject;

/**
 * [消息类型处理](http://doc.dev.yuanfenba.net/pkg/yuanfen/common/msg_data/)
 * Created by Kind on 2017/3/17.
 */
public class BaseMessage extends BaseMessageUserInfo implements IBaseMessage, Cloneable {

    @Override
    public BaseMessage parseJson(String jsonStr) {
        PLogger.d(jsonStr);
        this.setJsonStr(jsonStr);
        return this;
    }

    @Override
    public String getJson(BaseMessage message) {
        return null;
    }

    /**
     * 是否为发送者
     */
    public boolean isSender() {
        return App.uid == getSendID();
    }

    private int displayWidth = MsgUtils.getDisplayWidth();//消息显示的宽度，默认是80

    private long id;//自增ID
    private String channelID;// 频道ID 群ID
    private String whisperID;//私聊ID
    private long sendID;// 发送ID
    private long msgID = -1;//服务器消息ID
    private long cMsgID = -1;//客户端消息ID
    private long specialMsgID = -1;//特殊消息ID，对消息而言
    private long time;
    private String content;//具体内容
    private String jsonStr;//json串
    private int status;//1.发送成功2.发送失败3.发送中 10.未读11.已读//12未审核通过   私聊列表中是最后一条消息的状态
    private int fStatus = 1; // 给所有具有操作状态的消息用。1 表示可以操作；0 表示已经处理过
    private int type;//消息类型
    private int dataSource = 1;//数据来源 1.本地  2.网络  3.离线(默认是本地) 4.模拟消息
    private int version = 1;//版本
    private String msgDesc;//消息描述 mct
    private long know = MessageConstant.Know_stranger;//有know就不用ru了
    private boolean isSave;//是否保存

    //这几个字段 目前基本是给私聊用的
    private int num;//私聊列表专用字段
    private int Weight = MessageConstant.Small_Weight;//Item的权重
    private int MailItemStyle = MailItemType.Mail_Item_Ordinary.type;//私聊列表样式

    private String fContent;
    private int fPrivateNum;
    private int fGiftNum;

    public int getMailItemStyle() {
        return MailItemStyle;
    }

    public void setMailItemStyle(int mailItemStyle) {
        MailItemStyle = mailItemStyle;
    }

    /**
     * 初始化 MailItemStyle， Weight
     */
    public void initMailItemStyleWeight() {
        this.Weight = MessageConstant.Small_Weight;//Item的权重
        this.MailItemStyle = MailItemType.Mail_Item_Ordinary.type;//私聊列表样式
    }

    public String getfContent() {
        return fContent;
    }

    public JSONObject getfContentJSON() {
        return getJsonObject(fContent);
    }

    public void setfContent(String fContent) {
        this.fContent = fContent;
    }

    public int getfPrivateNum() {
        return fPrivateNum;
    }

    public void setfPrivateNum(int fPrivateNum) {
        this.fPrivateNum = fPrivateNum;
    }

    public int getfGiftNum() {
        return fGiftNum;
    }

    public void setfGiftNum(int fGiftNum) {
        this.fGiftNum = fGiftNum;
    }

    public int getWeight() {
        return Weight;
    }

    public void setWeight(int weight) {
        Weight = weight;
    }


    /***********************/
    private ChatPanelType msgPanelType;  // 消息面板用的：是否使用自定义消息面板。

    public int getDisplayWidth() {
        return displayWidth;
    }

    public void setDisplayWidth(int displayWidth) {
        this.displayWidth = displayWidth;
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getChannelID() {
        return channelID;
    }

    public void setChannelID(String channelID) {
        this.channelID = channelID;
    }

    public String getWhisperID() {
        return whisperID;
    }

    //转成LONG型
    public long getLWhisperID() {
        return TypeConvertUtil.toLong(whisperID);
    }

    public void setWhisperID(String whisperID) {
        this.whisperID = whisperID;
    }

    public String getSSendID() {
        return String.valueOf(sendID);
    }

    public long getSendID() {
        return sendID;
    }

    public void setSendID(long sendID) {
        this.sendID = sendID;
    }

    public long getTime() {
        return time;
    }

    public void setTime(long time) {
        this.time = time;
    }

    public boolean isSave() {
        return isSave;
    }

    public void setSave(boolean save) {
        isSave = save;
    }

    public String getJsonStr() {
        return jsonStr;
    }

    public JSONObject getJsonObj() {
        return getJsonObject(jsonStr);
    }

    public void setJsonStr(String jsonStr) {
        this.jsonStr = jsonStr;
    }

    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    public long getMsgID() {
        return msgID;
    }

    public void setMsgID(long msgID) {
        this.msgID = msgID;
    }

    public long getcMsgID() {
        return cMsgID;
    }

    public void setcMsgID(long cMsgID) {
        this.cMsgID = cMsgID;
    }

    public long getSpecialMsgID() {
        return specialMsgID;
    }

    public void setSpecialMsgID(long specialMsgID) {
        this.specialMsgID = specialMsgID;
    }

    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
    }

    public int getfStatus() {
        return fStatus;
    }

    public void setfStatus(int fStatus) {
        this.fStatus = fStatus;
    }

    public int getNum() {
        return num;
    }

    public void setNum(int num) {
        this.num = num;
    }

    public static long getCMsgID() {
        return MsgIDUtils.getMsgIDUtils().getMsgID();
    }

    public long getCurrentTime() {
        return ModuleMgr.getAppMgr().getTime();
    }

    public long getConversionTime(String date) {//转换时间
        return TimeUtil.stringTDateToMillisecond(date);
    }

    public boolean isCustomMsgPanel() {
        return ChatPanelType.CPT_Custom == msgPanelType;
    }

    public void setMsgPanelType(ChatPanelType msgPanelType) {
        this.msgPanelType = msgPanelType;
    }

    public ChatPanelType getMsgPanelType() {
        return msgPanelType;
    }

    public int getDataSource() {
        return dataSource;
    }

    public void setDataSource(int dataSource) {
        this.dataSource = dataSource;
    }

    public int getVersion() {
        return version;
    }

    public void setVersion(int version) {
        this.version = version;
    }

    public String getMsgDesc() {
        return msgDesc;
    }

    public void setMsgDesc(String msgDesc) {
        this.msgDesc = msgDesc;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public long getKnow() {
        return know;
    }

    public void setKnow(long know) {
        this.know = know;
    }

    @Override
    public Object clone() {
        BaseMessage o = null;
        try {
            o = (BaseMessage) super.clone();
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
        return o;
    }

    public BaseMessage() {
        super();
    }

    public BaseMessage(String channelID, String whisperID) {
        super();
        this.setChannelID(channelID);
        this.setWhisperID(whisperID);
        this.setSendID(App.uid);
        this.setTime(getCurrentTime());
        this.setcMsgID(getCMsgID());
        this.setMsgID(getcMsgID());
        PLogger.d("getCMsgID()=" + getcMsgID() + "");
    }

    //fmessage
    public BaseMessage(Bundle bundle) {
        this.setId(bundle.getLong(FMessage._ID));
        this.setChannelID(bundle.getString(FMessage.COLUMN_CHANNELID));
        this.setWhisperID(bundle.getString(FMessage.COLUMN_WHISPERID));
        this.setSendID(bundle.getLong(FMessage.COLUMN_SENDID));
        this.setMsgID(bundle.getLong(FMessage.COLUMN_MSGID));
        this.setcMsgID(bundle.getLong(FMessage.COLUMN_CMSGID));
        this.setSpecialMsgID(bundle.getLong(FMessage.COLUMN_SPECIALMSGID));
        this.setType(bundle.getInt(FMessage.COLUMN_TYPE));
        this.setStatus(bundle.getInt(FMessage.COLUMN_STATUS));
        this.setfStatus(bundle.getInt(FMessage.COLUMN_FSTATUS));
        this.setTime(bundle.getLong(FMessage.COLUMN_TIME));
        this.setJsonStr(bundle.getString(FMessage.COLUMN_CONTENT));
    }

    //私聊列表
    public BaseMessage(Bundle bundle, boolean fletter) {
        this.setId(bundle.getLong(FLetter._ID));
        this.setWhisperID(bundle.getString(FLetter.COLUMN_USERID));
        this.setInfoJson(bundle.getString(FLetter.COLUMN_INFOJSON));
        this.setType(bundle.getInt(FLetter.COLUMN_TYPE));
        parseInfoJson(this.getInfoJson());
        this.setKfID(bundle.getInt(FLetter.COLUMN_KFID));
        this.setStatus(bundle.getInt(FLetter.COLUMN_STATUS));
        this.setcMsgID(bundle.getLong(FLetter.COLUMN_CMSGID));
        this.setKnow(bundle.getInt(FLetter.COLUMN_RU));
        this.setTime(bundle.getLong(FLetter.COLUMN_TIME));
        this.setJsonStr(bundle.getString(FLetter.COLUMN_CONTENT));
        this.setMsgID(bundle.getLong(FLetter.COLUMN_MSGID));
        this.setNum(bundle.getInt(FLetter.Num));

        this.setfPrivateNum(bundle.getInt(FLetter.FPRIVATENUM));
//        String fContent = bundle.getString(FLetter.FCONTENT);
//        if (!TextUtils.isEmpty(fContent)) {
//            this.setfContent(fContent);
//        }

        this.setfGiftNum(bundle.getInt(FLetter.FGIFTNUM));
    }

    /**
     * 转换JSON 转子类的时候用
     */
    public void convertJSON(String jsonStr) {
        if (TextUtils.isEmpty(jsonStr)) return;
        this.setJsonStr(jsonStr);
    }
}